package com.wsc.wsckeeper.activities.fragments;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.database.SQLException;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.activities.AccountActivity;
import com.wsc.wsckeeper.activities.fragments.base.BaseFragment;
import com.wsc.wsckeeper.adapter.AccountsAdapter;
import com.wsc.wsckeeper.enums.Mode;
import com.wsc.wsckeeper.enums.OrderBy;
import com.wsc.wsckeeper.managers.ScreenManager;
import com.wsc.wsckeeper.model.entity.AccountE;
import com.wsc.wsckeeper.persistence.dao.AccountDAO;

public class AccountsFragment extends BaseFragment {
	private List<AccountE> accounts;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResume() {
		super.onResume();
		listView.invalidateViews();
	}

	public void sort(final String field) {
		final OrderBy order = model.getOrder();

		Collections.sort(accounts, new Comparator<AccountE>() {
			public int compare(AccountE a1, AccountE a2) {
				if (order.equals(OrderBy.ASC)) {
					if (AccountE.FIELD_DESCRIPTION.equals(field))
						return a1.getDescription().compareTo(a2.getDescription());
					else if (AccountE.FIELD_CREATED.equals(field))
						return a1.getCreated().compareTo(a2.getCreated());
					return a1.getLogin().compareTo(a2.getLogin());
				} else {
					if (AccountE.FIELD_DESCRIPTION.equals(field))
						return a2.getDescription().compareTo(a1.getDescription());
					else if (AccountE.FIELD_CREATED.equals(field))
						return a2.getCreated().compareTo(a1.getCreated());
					return a2.getLogin().compareTo(a1.getLogin());
				}
			}
		});

		model.setOrder(order.equals(OrderBy.ASC) ? OrderBy.DESC : OrderBy.ASC);

		onResume();
	}

	@Override
	public void initValues() {
		accounts = runtime.getAccounts();
		if (accounts == null) {
			try {
				accounts = AccountDAO.queryForAll();
				runtime.setAccounts(accounts);
			} catch (SQLException e) {
				showExceptionAsDialog(e);
			} catch (Exception e) {
				showExceptionAsDialog(e);
			}
		}

		if (accounts == null)
			accounts = new ArrayList<AccountE>();

		model.setOrder(OrderBy.ASC);
		sort(AccountE.FIELD_DESCRIPTION);

		listView.setAdapter(new AccountsAdapter(getActivity(), accounts));
		registerForContextMenu(listView);
	}

	@Override
	public void initListeners() {
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				model.setAccount(accounts.get(position));
				model.setAccountMode(Mode.SHOW);
				ScreenManager.getInstance().forward(getActivity(), AccountActivity.class);
			}
		});
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		AccountE account = accounts.get(info.position);
		switch (item.getItemId()) {
		case R.id.context_menu_edit: {
			model.setAccount(accounts.get(info.position));
			model.setAccountMode(Mode.EDIT);
			ScreenManager.getInstance().forward(getActivity(), AccountActivity.class);
			break;
		}
		case R.id.context_menu_delete: {
			try {
				runtime.getAccounts().remove(account);
				AccountDAO.delete(account);
				listView.invalidateViews();
			} catch (java.sql.SQLException e) {
				showExceptionAsDialog(e);
			} catch (Exception e) {
				showExceptionAsDialog(e);
			}
			break;
		}
		}
		return true;
	}
}
